/**
 * 
 */
package com.ls.fw.mvc.springmvc.support.freemaker.engine;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ls.fw.commons.freemaker.bean.FreeMarker;
import com.ls.fw.mvc.springmvc.support.freemaker.bean.GenControllerPara;

/**
 * dao模板实现s
 * @author Defender
 * 2014年2月27日 下午7:22:17
 */
public class ControllerMarker extends FreeMarker {
	
	public ControllerMarker(String dir){
		this(dir,"controller.ftl");
	}

	public ControllerMarker(String dir,String name){
		try {
			this.init(dir,name);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 
	
	public void buildTemplate(GenControllerPara para) throws Exception{
		Map<String, String> root = new HashMap<String, String>();
		root.put("entity", para.getEntityName());
		root.put("package", para.getServicePackage());
		root.put("vo", para.getVoName());
		root.put("vo_package", para.getVoPackage());
		root.put("form", para.getFormName());
		root.put("form_package", para.getFormPackage());
		root.put("entity_package", para.getEntityPackage());
		root.put("pk_type", para.getPkFullName());
		root.put("user", para.getUser());
		root.put("datetime", this.getNowDateString());
		root.put("service_name", para.getServiceName());
		root.put("service_package", para.getServicePackage());
		root.put("serviceParaName", para.getServiceParaName());
		root.put("mappingValue", para.getMappingValue());
		root.put("controller_name", para.getControllerName());
		root.put("controller_package", para.getControllerPackage());
	     String paraName = para.getEntityName().substring(0,1).toLowerCase()+para.getEntityName().substring(1);
		root.put("first_char_lower_entity", paraName);
		String fileName = para.getControllerName()+".java";
		String savePath = this.getSrcName(para.getSrcDir()) + File.separator
				+ StringUtils.replace(para.getControllerPackage(), ".", File.separator);
		if(para.isGen()){
			System.out.println("开始生成【"+para.getControllerPackage()+"."+fileName+"】...");
			this.changeTmplate("controller.ftl");
			this.buildTemplate(root, savePath, fileName);
			System.out.println("生成【"+para.getControllerPackage()+"."+fileName+"】成功！\n");
		}
	}

	public void buildTemplate(Class entity,Class vo,Class form
			, Class service, Class serviceImp,String mappingValue,String controllerPackage
			) throws Exception{
		    Service requestMapping = AnnotationUtils.findAnnotation(serviceImp, Service.class);
		    String servicePackage1 = "",serviceName = "",serviceParaName = "";
		    if (requestMapping != null && requestMapping.value().length() > 0) {
		    	serviceParaName = requestMapping.value();
	        }
		    servicePackage1 = service.getPackage().getName();
		    serviceName = service.getSimpleName();
		    this.buildTemplate(entity,vo,form, "", mappingValue,servicePackage1,serviceName,controllerPackage,true,serviceParaName);
	}
	
	public void buildTemplate(Class entity,Class vo,Class form
			, String servicePackage,String serviceName,String controllerPackage, String serviceParaName
			) throws Exception{
		this.buildTemplate(entity,vo,form, "", "",servicePackage,serviceName,controllerPackage, true,serviceParaName);
	}
	
	public void buildTemplate(Class entity,Class vo,Class form
			,String mappingValue,String servicePackage,String serviceName,String controllerPackage, String serviceParaName
			) throws Exception{
		this.buildTemplate(entity,vo,form, "", mappingValue,servicePackage,serviceName,controllerPackage, true,serviceParaName);
	}
	public void buildTemplate(Class entity,Class vo,Class form
			,String srcDir,String mappingValue,String servicePackage,String serviceName,String controllerPackage
			,boolean gen, String serviceParaName) throws Exception{
		this.buildTemplate(entity,vo,form, srcDir, mappingValue,servicePackage,serviceName,controllerPackage, "ls", gen,serviceParaName);
	}
	public void buildTemplate(Class entity,Class vo,Class form
			,String srcDir,String mappingValue,String servicePackage,String serviceName,String controllerPackage
			,String user,boolean gen, String serviceParaName) throws Exception{
		GenControllerPara p = new GenControllerPara();
		p.setEntityName(entity.getSimpleName());
		p.setEntityPackage(entity.getPackage().getName());
		p.setVoName(vo.getSimpleName());
		p.setVoPackage(vo.getPackage().getName());
		p.setFormName(form.getSimpleName());
		p.setFormPackage(form.getPackage().getName());
		p.setServiceName(serviceName);
		p.setServicePackage(servicePackage);
		p.setServiceParaName(serviceParaName);
		p.setGen(gen);
		p.setMappingValue(mappingValue);
		p.setControllerPackage(controllerPackage);
		if(org.apache.commons.lang3.StringUtils.isNotBlank(srcDir)){
			p.setSrcDir(srcDir);
		}
		this.buildTemplate(p);
	}
	 
}
